/* Copyright 2001,2002,2003 NAH6
 * All Rights Reserved
 *
 * Parts Copyright DoD, Parts Copyright Starium
 *
 */
          /*LINTLIBRARY*/
          /*PROTOLIB1*/
#include <math.h>
#include "main.h"
#include "acb_code.h"

static float ACBGainTable[32] =
{
  -0.993, -0.831, -0.693, -0.555, -0.414, -0.229,    0.0,  0.139,
   0.255,  0.368,  0.457,  0.531,  0.601,  0.653,  0.702,  0.745,
   0.780,  0.816,  0.850,  0.881,  0.915,  0.948,  0.983,  1.020, 
   1.062,  1.117,  1.193,  1.289,  1.394,  1.540,  1.765,  1.991
};

/**************************************************************************
*
* ROUTINE
*		ACBGainEncode
*
* FUNCTION
*
*		encode and quantize ACB gain for various
*		quantizer types.
*
* SYNOPSIS
*		ACBGainEncode(UGain, *QGain)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*	UGain		float	i	pitch gain input (true value)
*	QGain		int	o	encoded pitch gain index
*
*	ACBGainEncode	float	fun	encoded pitch gain
*
***************************************************************************
*
* DESCRIPTION
*
*	This funtion uses output level data obtained by Max's minimum
*	distortion quantization priciples and quantizes to the nearest
*	level (L1 norm).  (Using level data only was found superior to
*	using both of Max's level and boundry data.)
*
***************************************************************************
*
* CALLED BY
*
*	AdaptiveAnalysis Encode Decode
*
***************************************************************************
*
* REFERENCES
*
*	Quantizing for Minimum Distorion
*	J. Max
*	IRE Trans. Inform. Theory, vol. IT-6, pp.7-12, Mar. 1960
*
**************************************************************************/
float ACBGainEncode(
float 	UGain, 
int 	*QGain)
{
  int i;
  float dist, low;
  
  low = dist = fabs(UGain - *ACBGainTable);
  *QGain = 0;
  for (i = 1; i < 32; i++)
  {
    dist = fabs(UGain - ACBGainTable[i]);
    if (dist < low)
    {
      low = dist;
      *QGain = i;
    }
  }
  return (ACBGainTable[*QGain]);
}
/*

*************************************************************************
*
* ROUTINE
*		ACBGainDecode
*
* FUNCTION
*
*		decode ACB gain from table 
*
* SYNOPSIS
*		ACBGainDecode(QGain, UGain)
*
*   formal
*
*                       data    I/O
*       name            type    type    function
*       -------------------------------------------------------------------
*	QGain		int	i	Quantized ACB Gain
*	UGain		float	o	Unquantized ACB Gain
*
***************************************************************************
*
*	The data used in the table generation is from 3m3f.spd.
*
***************************************************************************
*
* REFERENCES
*
*	Quantizing for Minimum Distorion
*	J. Max
*	IRE Trans. Inform. Theory, vol. IT-6, pp.7-12, Mar. 1960
*
**************************************************************************/
void ACBGainDecode(
int 	QGain, 
float 	*UGain)
{
  *UGain = ACBGainTable[QGain];
}
